package cn.waka.sys.admin.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.waka.sys.admin.dao.ResourceDao;
import cn.waka.sys.admin.dao.ResourceDataDao;
import cn.waka.sys.admin.object.Page;
import cn.waka.sys.admin.object.ResourceData;
import cn.waka.sys.admin.object.Resources;
import cn.waka.sys.admin.service.ApiService.VideoService;

@Service
public class ResourceService {

    private ResourceDao dao;

    @Autowired
    private DicService dicService;

    @Autowired
    private ResourceDataDao dataDao;

    @Autowired
    private VideoService videoService;

    @Autowired
    public void setDao(ResourceDao dao) {
        this.dao = dao;
    }

    public int getResourceTotal(String field, String value) {
        return dao.getResourceTotal(field, value);
    }

    public Map<String, Object> getResourceInfo(int start, int size) {
        return dao.getResourceList(start, size);
    }

    public int getVideoTotal(int resource_id) {
        return dao.getVideoTotal(resource_id);
    }

    public int updateSelected(int id, int is_selected) {
        return dao.updateSelected(id, is_selected);
    }

    public Page getSchoolResources(Page p) {
        List<Object> list = dao.getSchoolResources(p);//总列表
        int count = dao.findCount(p);
        p.setData(list);
        p.setCount(count);
        int totalCount = 0;
        if (count % p.getSize() > 0) {
            totalCount = (int) Math.floor(count / p.getSize());//总页数
            totalCount++;
        } else {
            totalCount = count / p.getSize();
        }
        if (totalCount == 0) {
            totalCount = 1;
        }
        p.setTotalPage(totalCount);
        return p;
    }

    public Page findResoucePage(Page p, Map<String, String> param) {
        Map<Integer, String> dicMap = dicService.getDicMap();
        List<Map<String, Object>> list = dao.findList(p, param);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).put("source", dicMap.get(list.get(i).get("source")));
            list.get(i).put("region", dicMap.get(list.get(i).get("region")));
            list.get(i).put("language", dicMap.get(list.get(i).get("language")));
            list.get(i).put("type", dicMap.get(list.get(i).get("type")));
            list.get(i).put("resource_num", dao.getResourceNum((int) list.get(i).get("id")));
        }
        int count = dao.findCount(p, param);
        p.setData(list);
        p.setCount(count);
        int totalCount = 1;
        if (count % p.getSize() > 0) {
            totalCount = (int) Math.floor(count / p.getSize());
            totalCount++;
        } else {
            totalCount = count / p.getSize();
        }
        p.setTotalPage(totalCount);
        return p;
    }

    public Resources findById(int id) {
        return dao.findById(id);
    }

    public int save(Resources r) {
        int rs = 1;
        if (r.getId() > 0) {
            rs = dao.update(r);
        } else {
            rs = dao.insert(r);
        }
        return rs;
    }

    public int del(int id) {
        int rs = dao.del(id);
        if (rs > 0) {
            dao.delDataByResource(id);
        }
        return rs;
    }

    public int updateStatus(int id, int status) {
        return dao.updateStatus(id, status);
    }

    public Page findResouceDataPage(Page p, int resource_id) {
        int page = p.getPage();
        int start = (page - 1) * p.getSize();
        int size = p.getSize();
        List<ResourceData> list = dataDao.findPage(resource_id, start, size);
        int count = dataDao.findTotal(resource_id);
        p.setCount(count);
        p.setData(list);
        int totalPage = 0;
        if (count > 0 && count % p.getSize() > 0) {
            totalPage = (int) Math.floor(count / p.getSize());
            totalPage++;
        } else {
            totalPage = count / p.getSize();
        }
        if (totalPage == 0) {
            totalPage = 1;
        }
        p.setTotalPage(totalPage);

        return p;
    }

    public ResourceData findDataById(int id) {
        return dataDao.findById(id);
    }

    public int saveData(ResourceData r) {
        int rs = 1;
        if (r.getId() > 0) {
            rs = dataDao.update(r);
        } else {
            //获取当前最大序号
            Object maxSort = dataDao.maxSort(r.getResourceId());
            if (maxSort == null) {
                maxSort = 0;
            }
            r.setSort((int) maxSort + 1);
            rs = dataDao.save(r);
        }
        return rs;
    }

    public Page findPage(Page p) {
        List<Map<String, Object>> list = dao.findList(p, null);
        int count = dao.findCount(p, null);
        p.setData(list);
        p.setCount(count);
        int totalCount = 1;
        if (count % p.getSize() > 0) {
            totalCount = (int) Math.floor(count / p.getSize());
            totalCount++;
        } else {
            totalCount = count / p.getSize();
        }
        p.setTotalPage(totalCount);
        return p;
    }

    public Page resourcePlayLogs(Page p, Map<String, String> param) {
        List<Map<String, Object>> list = dao.resourcePlayLogs(p, param);
        int count = dao.playLogCount(p, param);
        p.setData(list);
        p.setCount(count);
        int totalCount = 1;
        if (count % p.getSize() > 0) {
            totalCount = (int) Math.floor(count / p.getSize());
            totalCount++;
        } else {
            totalCount = count / p.getSize();
        }
        p.setTotalPage(totalCount);
        return p;
    }

    public int delData(int id) {
        ResourceData r = dataDao.findById(id);
        int resourceId = r.getResourceId();
        int sort = r.getSort();
        dataDao.top_dec(resourceId, sort);
        return dao.delData(id);
    }

    public int sortData(int id, String action) {
        int a = 0;
        ResourceData r = dataDao.findById(id);
        int resourceId = r.getResourceId();
        int sort = r.getSort();
        Object _maxSort = dataDao.maxSort(resourceId);
        int maxSort = 1;
        if (_maxSort == null) {
            maxSort = 0;
        } else {
            maxSort = (int) _maxSort;
        }
        if (action.equals("top") && sort < maxSort) {
            dataDao.top_dec(resourceId, sort);
            a = dataDao.sort(id, maxSort);
        } else if (action.equals("up") && sort < maxSort) {
            dataDao.up_dec(resourceId, sort + 1);
            a = dataDao.sort(id, sort + 1);
        } else if (action.equals("down") && sort > 1) {
            dataDao.down_inc(resourceId, sort - 1);
            a = dataDao.sort(id, sort - 1);
        }
        return a;
    }

    public int add_index(int id) {
        try {
            Resources r = dao.findById(id);
            videoService.add(id + "", r.getTitle(), r.getImg());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }

    public int del_index(int id) {
        try {
            videoService.delete(id + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }
}
